package com.likou.math;

/**
 * Created by HXT on 2019/7/30.
 */
public class Solution_2 {
    /**
     * 一个“快乐数”定义为：对于一个正整数，
     * 每一次将该数替换为它每个位置上的数字的平方和，
     * 然后重复这个过程直到这个数变为 1，也可能是无限循环但始终变不到 1。
     * 如果可以变为 1，那么这个数就是快乐数。
     * @param n
     * @return
     */
    public boolean isHappy(int n) {

        int result = 0;
        int temp = n;
        do{
            while(n!=0)
            {
                int digit =  n%10;
                result = result+digit*digit;
                n/=10;
            }
            if(result == 1 ) return true;
            n = result;
            result = 0;
        }while(n!= 20);
        return false;
           /* String[] false_result = {"80","81","78","77","76","75","74","73","72","71","83","69","84","67","66","65","64","63","62","61","60","59","58","57","56","55","54","53","52","51","50","85","48","47","46","45","87","43","42","41","40","39","38","37","36","35","34","33","88","89","30","29","90","27","26","25","24","92","22","21","20","93","18","17","16","15","14","95","12","11","96","9","8","98","6","5","4","3","2","99","0"};
            Set staffsSet = new HashSet<>(Arrays.asList(false_result));
            while (true) {
                if (n < 100) {
                    return !staffsSet.contains(n+"");
                }
                n = format_is_happy(n);
            }*/
        }

    public int format_is_happy(int n){
        int result = 0;
        do{
            result += (n%10)*(n%10);
            n=n/10;
        }while(n!=0);
        return result;
    }

    public static void main(String[] args){
        Solution_2 solution_2 = new Solution_2();
        System.out.println(solution_2.isHappy(19));
       /* for (int i = 0; i < 100; i++) {
            System.out.print(i+"-");
            System.out.println(solution_2.isHappy(i));
        }*/
    }
}
